package com.csy.user.controller;

import java.time.Duration;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.csy.commons.constant.Constants;
import com.csy.commons.core.utils.ChainedMap;
import com.csy.commons.core.utils.UUIDUtils;
import com.wf.captcha.SpecCaptcha;

/**
 * 获取验证码
 * @Auther: sunxx
 * @Date: 2024/1/11 - 01 - 11 - 23:04
 * @Description: com.sxx.ajcommunity.web.controller.common
 * @version: 1.0
 */

@RestController
@RequestMapping
public class CaptchaController {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/captchaImage")
    public ChainedMap getCode(HttpServletResponse response){
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);

        //生成验证码，以及验证码唯一标识
        String uuid = UUIDUtils.simpleUUID();

        //拼接redis的key
        String key = Constants.CAPTCHA_CODE_KEY + uuid;
        String code = specCaptcha.text().toLowerCase();

        //保存到redis
        redisTemplate.opsForValue().set(key,code, Duration.ofMinutes(10));

        return ChainedMap.create().set("uuid",uuid).set("img",specCaptcha.toBase64());
    }

}
